﻿
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;

namespace server
 
{
    class Program
    {
        static void Main(string[] args)
       {
           Encoding ascii = Encoding.ASCII;
           Encoding unicode = Encoding.Unicode;


            int port = 1234;
            string host = "192.168.6.126";
            // Console.WriteLine("等待客户端连接,请输入服务器IP地址：");
            // host = Console.ReadLine();
            Console.WriteLine("等待客户端连接,请输入服务器监听端口port地址：");
           // port = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("服务器端IP地址:{0},监听端口地址:{1}", host, port);


            /**////创建终结点（EndPoint）
            IPAddress ip  = IPAddress.Parse(host);//把ip地址字符串转换为IPAddress类型的实例
            IPEndPoint ipe = new IPEndPoint(ip, port);//用指定的端口和ip初始化IPEndPoint类的新实例

         /**////创建socket并开始监听
             ///   
         try
          {
            
            Console.WriteLine("等待客户端连接....connect...waiting...");
            Socket s　=　new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//创建一个socket对像，如果用udp协议，则要用SocketType.Dgram类型的套接字
            s.Bind(ipe);//绑定EndPoint对像（2000端口和ip地址）
            s.Listen(0);//开始监听


         /**////接受到client连接，为此连接建立新的socket，并接受信息
            Socket tempsocket = s.Accept();//为新建连接创建新的socket
            Console.WriteLine("建立连接,，已有客户端连上");


                string recvStr = "";
                byte[] recvBytes = new byte[30] ;
                //没有初始化缓冲区
                Array.Clear(recvBytes,0, recvBytes.Length);
                int bytes;
                while (true) 
                {
                    Array.Clear(recvBytes, 0, recvBytes.Length);

                    bytes = tempsocket.Receive(recvBytes, recvBytes.Length, 0);//从客户端接受信息
                    recvStr += Encoding.Unicode.GetString(recvBytes, 0, bytes);


                    /**/
                    ///给client端返回信息
               

                    Console.WriteLine("server get message:{0}", recvStr);//把客户端传来的信息显示出来
                    recvStr = unicode.GetString(recvBytes);
                    Console.WriteLine("server get message:{0}", recvStr);//把客户端传来的信息显示出来
                    recvStr = "";

                    //  char c = '\0';
                    //  int length = 20;
                    // string  sendStr =new string(c,length);
                    // sendStr = "ok";
                    string sendStr = "你ok好0";



                    //   byte[] bs = Encoding.ASCII.GetBytes(sendStr);
                    //   Console.WriteLine("send back:string:ok");


                    byte[] bs = Encoding.Unicode.GetBytes(sendStr);
                    Console.WriteLine("send back:{0}", sendStr);

                    tempsocket.Send(bs, bs.Length, 0);//返回信息给客户端
                    Console.WriteLine("send ok sucesss ");
                    //tempsocket.Close();
                    //Console.ReadLine();
                }
               
            }
            catch (SocketException e)
            {
               // Console.WriteLine("{0} Error code: {1}.", e.ErrorCodee.get_Message(),(Int32)e.get_ErrorCode());
                Console.WriteLine("{0} Error code: {1}.", e.Message, (Int32)e.ErrorCode); 

                //return e.get_ErrorCode();
                Console.ReadLine();
            }



        }
       
    }
}